package io.renren.modules.maintenance.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.modules.maintenance.entity.MDeviceCategory;
import io.renren.modules.maintenance.entity.MDeviceDetail;
import io.renren.modules.maintenance.entity.MGuest;
import io.renren.modules.maintenance.entity.MOrder;
import io.renren.modules.maintenance.mapper.MOrderMapper;
import io.renren.modules.maintenance.service.*;
import io.renren.modules.maintenance.util.OrderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 * <p>
 * ！！！！！！！！！！！！仍需添加自定义异常！！！！！！！！
 *
 * @author iweb
 * @since 2020-08-07
 */
@Service
public class MOrderServiceImpl extends ServiceImpl<MOrderMapper, MOrder> implements IMOrderService {

    @Autowired
    private IMGuestService mGuestService;
    @Autowired
    private IMDeviceCategoryService mDeviceCategoryService;
    @Autowired
    private IMOrderService mOrderService;
    @Autowired
    private IMWorkSheetService mWorkSheetService;
    @Autowired
    private IMDeviceDetailService mDeviceDetailService;

    /**
     * 返回所有客户以及设备信息
     *
     * @return
     */
    @Override
    public Map<String,List> getAllGuestAndDev() {

        List<MGuest> guestList = mGuestService.getAll();
        List<MDeviceCategory> deviceCategoryList = mDeviceCategoryService.getAll();

        HashMap<String, List> allGuestAndDev = new HashMap<>();
        allGuestAndDev.put("allGuest",guestList);
        allGuestAndDev.put("allDevCate",deviceCategoryList);
        return allGuestAndDev;
    }

    /**
     * @Transactional 事务回滚，当该注解下的方法抛出异常时 ，回滚事务
     *      ！！！！！后续添加自定义异常
     * @param order order中只有前端传递的guestId、devCateId、engineerName
     * @return
     */
    @Override
    @Transactional
    public boolean insertOrder(MOrder order) {
        MOrder mOrder = order;
        Date date = new Date();//订单生成时间
        String orderId = OrderUtil.generateOrderNumber();//生成订单号

        MGuest guestById = mGuestService.getById(order.getGuestId());//获取对应的客户信息

        mOrder.setDate(date);
        mOrder.setOrderId(orderId);
        mOrder.setMGuest(guestById);

        /*
        仍需添加自定义异常
         */
        /**
         * 在生成订单的同时，生成对应的工作单
         */
        if (mOrderService.save(mOrder)) {//如果订单数据插入成功，创建对应工作单的设备维护信息
            //创建设备维护信息实体类,填写客户名，设备类别id，工程师名
            MDeviceDetail deviceDetail = new MDeviceDetail();
            deviceDetail.setCompany(guestById.getCompany());
            deviceDetail.setDevCateId(mOrder.getDevCateId());
            deviceDetail.setEngineerName(mOrder.getEngineerName());

            if (mDeviceDetailService.insertDeviceDetail(deviceDetail)) {//如果设备维护信息插入成功，创建对应工作单

                //插入工作单
                if(mWorkSheetService.insertWorkSheet(orderId,mOrder.getDevCateId())){
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 返回所有订单
     *
     * @return
     */
    @Override
    public List<MOrder> getAllOrder() {

        List<MOrder> orderList = this.list();
        return orderList;
    }
}
